<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/6/14
 * Time: 17:16
 */
namespace Admin\Controller;

class ContentController extends AdminBase{

    //当前栏目id
    public $catid = 0;
    //模型缓存
    protected $model = array();

    //初始化
    public function _initialize() {
        parent::_initialize();
        $this->catid = I('request.catid', $_POST['info']['catid'], 'intval');
        //合并缓存content_form
        $this->content_form_cache();
     }
    //合并缓存 ldh
    private function content_form_cache(){
        $input_php=RUNTIME_PATH.'content_form.class.php';
        if(!file_exists($input_php)){
            $out_php=MODULE_PATH.'Fields/';
            $dirs=scandir($out_php);
            if(is_array($dirs)){
                $php=file_get_contents($out_php.'/content_form.class.php');
                $php=str_replace('}##{del}##','',$php);
                file_put_contents($input_php,$php);
                foreach($dirs as $k=> $v){
                    if(is_dir($out_php.$v) || $v!="." || $v!=".."){
                        if(file_exists($out_php.$v.'/form.inc.php')){
                            $php=file_get_contents($out_php.$v.'/form.inc.php');
                            $php=str_replace('<?php','',$php);
                            file_put_contents($input_php,$php,FILE_APPEND);
                        }
                    }
                }
                file_put_contents($input_php,"\n}",FILE_APPEND);
            }
        }

    }

    //标题检查
    public function public_check_title() {
        $title = I('get.data');
        $catid = $this->catid;
		
        if (empty($title)) {
            $this->ajaxReturn(array('status' => 1, 'info' => '标题没有重复！'));
            return false;
        }
        $tablename=M('model')->where(array('modelid'=>getCategory($catid, 'modelid')))->getField('tablename');
        $count = M($tablename)->where(array('title' => $title))->count();
        if ($count > 0) {
            $this->ajaxReturn(array('status' => 0, 'info' => '标题有重复！'));
        } else {
            $this->ajaxReturn(array('status' => 1, 'info' => '标题没有重复！'));
        }
    }
    public function index(){

        $catelist=M('category')->field('catid,parentid,catname,cont')->order('listorder ASC')->select();

        $this->assign('catelist',$catelist);
        $this->display();
    }
    //内容列表
    public function lists(){

        if(!$this->catid){ $this->Thisreturn(false,'','栏目id不存在！');}
        $where['catid']=$this->catid;
        $start=I('get.start');
        $end=I('get.end');
        $keywords=I('get.kw');
        if($start){
            $where['inputtime']=array('gt',strtotime($start));
            if($end){
                $where['inputtime']=array(array('gt',strtotime($start)),array('lt',strtotime($end)));
            }
        }
        if($keywords){
            $where['title']=array('like',"%{$keywords}%");
        }
 		$category = getCategory($this->catid);
		 
        $models=getModel($category['modelid']);
        $list=$this->pages($models['tablename'],$where,'','listorder DESC');
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('keywords',$keywords);
        $this->assign('list',$list);
        $this->assign('catid',$this->catid);
        $this->display();
    }

    //编辑内容
    public function edit(){
        $id=I('get.id',0,'int');
        $catid =  $this->catid;
        if (empty($catid)) {
            $this->error("请指定栏目ID！");
        }
        //获取当前栏目配置
        $category = getCategory($catid);
        $models=getModel($category['modelid']);
        if($id){
            $count=M($models['tablename'])->where(array('catid'=>$catid,'id'=>$id))->count();
        }
        if (IS_POST) {

            if (trim($_POST['info']['title']) == '') {
                $this->error("标题不能为空！");
            }
            //栏目类型为0
            if ($category['type'] == 0) {

                //检查模型是否被禁用
                if ($models['disabled'] == 1) {
                    $this->error("模型被禁用！");
                }

                $pages=$_POST['info']['pages'];
                unset($_POST['info']['pages']);
                $info=$_POST['info'];
                unset($_POST['info']);
                $data=array_merge($info,$pages,$_POST);
                $data['inputtime']=$data['inputtime']?strtotime($data['inputtime']):time();
                $data['updatetime']=time();

                M($models['tablename'])->create($data);
                //编辑
                if($id && $count){
                    $wh['id']=$id;
                    if(M($models['tablename'])->where($wh)->save()){
                        $data['id']=$id;
                        M($models['tablename'].'_data')->create($data);
                        if(M($models['tablename'].'_data')->where($wh)->count()){
                            M($models['tablename'].'_data')->where($wh)->save();
                        }else{
                            M($models['tablename'].'_data')->add();
                        }

                        $this->ThisReturn(true);
                    }else{
                        $this->ThisReturn(false);
                    }

                }else{
                    //添加
                    $content_id=M($models['tablename'])->add();
                    if($content_id){
                        $data['id']=$content_id;
                        M($models['tablename'].'_data')->create($data);
                        $this->ThisReturn(M($models['tablename'].'_data')->add());
                    }else{
                        $this->ThisReturn(false);
                    }

                }



            } else if ($category['type'] == 1) {

            } else {
                $this->error("该栏目类型无法发布！");
            }
        } else {

            if (empty($category)) {
                $this->error('该栏目不存在！');
            }
            if ($category['type'] == 0) {


                //检查模型是否被禁用
                if ($models['disabled'] == 1) {
                    $this->error('该模型已被禁用！');
                }
                //实例化表单类 传入 模型ID 栏目ID 栏目数组

                include_once(RUNTIME_PATH.'content_form.class.php');
                $content_form = new \content_form($models['modelid'], $this->catid);
                if($id){
                $tablename=strtoupper($models['tablename']);
                $Model = M($models['tablename']);
                $data=$Model->join('LEFT JOIN __'.$tablename.'_DATA__  ON  __'.$tablename.'__.id = __'.$tablename.'_DATA__.id where __'.$tablename.'__.catid = '.$this->catid.' and __'.$tablename.'__.id ='.$id)->find();

                  }
                //生成对应字段的输入表单
                $forminfos = $content_form->get($data);
                //生成对应的JS验证规则
                $formValidateRules = $content_form->formValidateRules;
                //js验证不通过提示语
                $formValidateMessages = $content_form->formValidateMessages;
                //js
                $formJavascript = $content_form->formJavascript;
                //取得当前栏目setting配置信息
                $setting = $category['setting'];

                $this->assign("catid", $this->catid);
                $this->assign("content_form", $content_form);
                $this->assign("forminfos", $forminfos);
                $this->assign("formValidateRules", $formValidateRules);
                $this->assign("formValidateMessages", $formValidateMessages);
                $this->assign("formJavascript", $formJavascript);
                $this->assign("setting", $setting);
                $this->assign("category", $category);
                $this->display();
            }
        }
    }

    public function status(){

    if(IS_AJAX){
        $status=I('get.status',0,'int');
        $id=I('get.id',0,'int');
        $catid=I('get.catid',0,'int');
        $tablename=getmodel(getCategory($catid,'modelid'),'tablename');
        if(!$tablename){
            $this->ajaxReturn(validreturn(false,'',$tablename.'表不存在！'));
        }
        $where['catid']=$catid;
        $where['id']=$id;
        $table=M($tablename);
        if(!$table->where($where)->count()){
            $this->ajaxReturn(validreturn(false,'','信息不存在！'));
        }
        if(empty($status)){
            $res=$table->where($where)->save(array('status'=>99));
            $this->ajaxReturn(validreturn($res));

        }else{
            $res=$table->where($where)->save(array('status'=>0));

            $this->ajaxReturn(validreturn($res));

        }


    }


    }
    //删除内容
    public function del(){

        if(IS_AJAX){

            $id=I('get.id',0,'int');
            $ids=I('post.id');
            $catid=I('get.catid',0,'int');
			
            $tablename=getmodel(getCategory($catid,'modelid'),'tablename');
            if(!$tablename){
                $this->ajaxReturn(validreturn(false,'',$tablename.'表不存在！'));
            }

            $where['catid']=$catid;
            if($ids){
                $where['id']=array('in',$ids);
            }else{
                $where['id']=$id;
            }

            $table=M($tablename);
            if(!$table->where($where)->count()){
                $this->ajaxReturn(validreturn(false,'','信息不存在！'));
            }

            $res=$table->where($where)->delete();
            $this->ajaxReturn(validreturn($res));

 

        }


    }


}